package com.org.utils.string;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 字符串处理工具了
 * 
 * 
 */
public abstract class StringUtils {

	private static List<File> fs = new ArrayList<File>(0);

	/**
	 * 检查指定的字符串是否为空。
	 * 
	 * @param value
	 *            待检查的字符串
	 * @return true/false
	 */
	public static boolean isEmpty(String value) {
		int strLen;
		if (value == null || (strLen = value.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if ((Character.isWhitespace(value.charAt(i)) == false)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 检查对象是否为数字型字符串,包含负数开头的。
	 */
	public static boolean isNumeric(Object obj) {
		if (obj == null) {
			return false;
		}
		char[] chars = obj.toString().toCharArray();
		int length = chars.length;
		if (length < 1)
			return false;

		int i = 0;
		if (length > 1 && chars[0] == '-')
			i = 1;

		for (; i < length; i++) {
			if (!Character.isDigit(chars[i])) {
				return false;
			}
		}
		return true;
	}

	public static String valueOf(Object object, String defaultEmptyValue) {

		if (object == null) {
			return defaultEmptyValue;
		}

		return String.valueOf(object);
	}

	public static String valueOf(Object object) {

		return valueOf(object, "");
	}

	public static boolean isEmptyOrNull(Object object) {

		if (StringUtils.valueOf(object).equals("")) {
			return true;
		}

		return false;
	}

	/**
	 * 检查指定的字符串列表是否不为空。
	 */
	public static boolean areNotEmpty(String... values) {
		boolean result = true;
		if (values == null || values.length == 0) {
			result = false;
		} else {
			for (String value : values) {
				result &= !isEmpty(value);
			}
		}
		return result;
	}

	/**
	 * 把通用字符编码的字符串转化为汉字编码。
	 */
	public static String unicodeToChinese(String unicode) {
		StringBuilder out = new StringBuilder();
		if (!isEmpty(unicode)) {
			for (int i = 0; i < unicode.length(); i++) {
				out.append(unicode.charAt(i));
			}
		}
		return out.toString();
	}

	/**
	 * 过滤不可见字符
	 */
	public static String stripNonValidXMLCharacters(String input) {
		if (input == null || ("".equals(input)))
			return "";
		StringBuilder out = new StringBuilder();
		char current;
		for (int i = 0; i < input.length(); i++) {
			current = input.charAt(i);
			if ((current == 0x9) || (current == 0xA) || (current == 0xD)
					|| ((current >= 0x20) && (current <= 0xD7FF))
					|| ((current >= 0xE000) && (current <= 0xFFFD))
					|| ((current >= 0x10000) && (current <= 0x10FFFF)))
				out.append(current);
		}
		return out.toString();
	}

	/**
	 * 判断字符串是否是数值
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isDigit(String str) {
		boolean isDigit = false;

		// 判断是否为null
		if (str == null || "".equalsIgnoreCase(str.trim())) {
			return isDigit;
		}
		// 判断依据-正则
		Pattern pattern = Pattern.compile("[0-9]*");
		isDigit = pattern.matcher(str).matches();
		return isDigit;
	}
	
	/**
	 * 正则表达式判断一个字符串是否是一个数字类型，支持正号和负号
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str)
	{
		return str.matches("[+-]?\\d*(\\.\\d+)?");
	}

	/**
	 * 删除字符中所有空格
	 * 
	 * @param str
	 * @return
	 */
	public static String deleAllBlank(String str) {
		if (str == null) {
			return "";
		} else {
			return str.replace(" ", "");
		}
	}

	/**
	 * 读取文件
	 * 
	 * @param src
	 * @return
	 */
	public static String read(File src) {
		StringBuffer res = new StringBuffer();
		String line = null;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(src));
			while ((line = reader.readLine()) != null) {
				res.append(line + "\n");
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return res.toString();
	}

	/**
	 * 写入文件
	 * 
	 * @param src
	 * @return
	 */
	public static boolean write(String cont, File dist) {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(dist));
			writer.write(cont);
			writer.flush();
			writer.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	/***
	 * 过滤html标签
	 * @param input
	 * @return
	 */
	public static String filterHtml(String input) {
		if (input == null) {
			return null;
		}
		if (input.length() == 0) {
			return input;
		}
		input = input.replaceAll("&", "&amp;");
		input = input.replaceAll("<", "&lt;");
		input = input.replaceAll(">", "&gt;");
		input = input.replaceAll(" ", "&nbsp;");
		input = input.replaceAll("'", "&#39;");
		input = input.replaceAll("\"", "&quot;");
		return input.replaceAll("\n", "<br>");
	}
	/**
	 * 递归文件
	 * 
	 * @param file
	 * @return
	 */
	public static List<File> getFiles(File dir) {

		if (dir == null) {
			return null;
		}
		if (!dir.isDirectory()) {
			return null;
		}
		File[] files = dir.listFiles();
		for (File file : files) {
			if (file.isFile()) {
				fs.add(file);
			} else if (file.isDirectory()) {
				getFiles(file); // 如果遇到目录则通过递归调用继续统计
			}
		}
		return fs;
	}

	/**
	 * 写入文件
	 * 
	 * @param file
	 * @return
	 */
	public static void replaceStr(String file, String originStr,
			String replaceStr) {
		File src = new File(file);
		List<File> fs1 = new ArrayList<File>(0);
		fs1 = getFiles(src);
		for (File f : fs1) {
			String cont = StringUtils.read(f);
			// 对得到的内容进行处理
			cont = cont.replaceAll(originStr, replaceStr);
			StringUtils.write(cont, f);
		}
	}
	/**
	 * 找出字符串中所有指定的字符串
	 * 
	 * @param str
	 *            传入的字符串
	 * @param regex
	 *            正则表达式
	 * @return
	 */
	public static List<String> findAllStr(String str, String regex) {
		if (str == null || str.equals("") || regex == null) {
			return null;
		}
		List<String> result = new ArrayList<String>(0);
		Pattern pattern = Pattern.compile(regex);
		Matcher m = pattern.matcher(str);
		while (m.find()) {
			String g = m.group();
			result.add(g);
		}
		return result;
	}
	 

	public static boolean isNotNull(String string)
	{
		boolean flog = false;
		if (string != null && !string.trim().isEmpty())
		{
			flog = true;
		}
		return flog;
	}

}
